import Moya

enum CameraService {
    case getOSSPolicy
    case getOSSUrl(filename:String)
    case getSkinTest(url:String)
}

extension CameraService: TargetType {
    

    var baseURL: URL {
        return URL(string: "http://106.14.226.26:8080/fun-skincare")!
    }

    var path: String {
        switch self {
        case .getOSSUrl:
            return "/oss/get-url"
        case .getOSSPolicy:
            return "/oss/getOssSign"
        case .getSkinTest:
            return "/oss/skin-test"
        }
    }

    var method: Method {
        switch self {
        case .getOSSUrl:
            return .post
        case .getOSSPolicy:
            return .get
        case .getSkinTest:
            return .post
        }
    }

    var sampleData: Data {
        return Data()
    }

    var task: Task {
        switch self {
        case .getOSSUrl(let filename):
            return .requestParameters(parameters: ["filename": filename], encoding: JSONEncoding.default)
        case .getOSSPolicy:
            return .requestPlain
        case .getSkinTest(let url):
            return .requestParameters(parameters: ["url": url], encoding: JSONEncoding.default)
        }
        //表示没有参数的网络请求
        //case .getUsers(let userId):
       // return .requestParameters(parameters: ["userId": userId], encoding: JSONEncoding.default)
    }

    var headers: [String : String]? {
        return ["Content-type": "application/json"]
    }
}

let endpointClosure = { (target: CameraService) -> Endpoint in
    let defaultEndpoint = MoyaProvider.defaultEndpointMapping(for: target)
    let token = getToken()
    return defaultEndpoint.adding(newHTTPHeaderFields: ["Authorization": "Bearer \(token)"])
}

let camera_provider = MoyaProvider<CameraService>(endpointClosure: endpointClosure)

